package com.hwj.controller;

import com.hwj.entity.PostType;
import com.hwj.service.PostTypeService;
import com.hwj.utils.JsonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/postType")
public class PostTypeController {
    @Autowired
    PostTypeService postTypeService;

    /*前台后台查询*/
    @GetMapping("/select")
    public JsonResponse selectAll(){
        return JsonResponse.successResponse(0,"查询帖子类型成功",postTypeService.select());
    }

    /*后台新增和更新*/
    @PostMapping("/")
    public JsonResponse updateAndInsert(@RequestBody PostType postType){
        if(postTypeService.updateAndInsert(postType)==1){
            return JsonResponse.successResponse(0,"更新或提交成功",null);
        }
        else {
            return JsonResponse.failResponse(-2,"更新或提交失败，请重新尝试！",null);
        }
    }

    /*后台删除*/
    @DeleteMapping("/{postTypeId}")
    public JsonResponse delete(@PathVariable Long postTypeId){
        if(postTypeService.deleteByPrimaryKey(postTypeId)==1){
            return JsonResponse.successResponse(0,"删除成功",null);
        }else{
            return JsonResponse.failResponse(-2,"删除失败，请重新尝试！",null);
        }
    }

    /*后台分页查询*/
    @GetMapping("/selectByPage/{pageNum}/{pageSize}")
    public JsonResponse selectByPage(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize,@RequestParam(value="postTypeName",required = false) String postTypeName){
        return JsonResponse.successResponse(0,"分页查询成功",postTypeService.selectByPage(pageNum,pageSize,postTypeName));
    }
}
